package mp

import (
	"duoke/internal/dao"
	"duoke/model"
	"duoke/service"
	"duoke/tables"
	"duoke/utils/gconv"
	"duoke/utils/request"
	"duoke/utils/response"
	"duoke/utils/valid"
	"github.com/gin-gonic/gin"
	"github.com/go-playground/validator/v10"
)

var Separate = new(separateCtl)

type separateCtl struct{}

// 用户绑定商户号
func (ctl *separateCtl) Bind(c *gin.Context) {
	// 1查询该用户是否绑定商户
	req := request.JsonMap(c)
	user, err := service.User.Info(c, "sub_mch_id,openid")
	if err != nil {
		response.Error(c, err.Error(), nil)
		return
	}
	if user.SubMchId != "" {
		response.Error(c, "该用户已经绑定商户了", nil)
		return
	}
	subMchId := gconv.String(req["sub_mch_id"])
	_, err = service.SeparateAccounts.Info(c, "id", subMchId)
	if err != nil {
		response.Error(c, err.Error(), nil)
		return
	}
	// 这里开始绑定 不能走线程 因为要返回信息给用户
	openId := user.Openid
	if openId == "" {
		response.Error(c, "请填写用户openid", nil)
		return
	}
	err = service.SeparateAccountsSub.BindUser(c, model.ReqSeparateAccountsSub{
		SubMchId: subMchId,
		Account:  openId,
		Type:     "PERSONAL_OPENID",
	})
	if err != nil {
		response.Error(c, err.Error(), nil)
		return
	}
	// 绑定成功后 修改用户表
	err = service.User.UpdateInfo(c, model.ReqUser{
		SubMchId: subMchId,
	})
	if err != nil {
		response.Error(c, err.Error(), nil)
		return
	}
	response.Success(c, "success", gin.H{})
}

// 获取商户信息
func (ctl *separateCtl) Info(c *gin.Context) {
	req := request.JsonMap(c)
	subMchId := gconv.String(req["sub_mch_id"])
	info, err := service.SeparateAccounts.Info(c, "id,sub_mch_name,name,mobile", subMchId)
	if err != nil {
		response.Error(c, err.Error(), nil)
		return
	}
	response.Success(c, "success", gin.H{
		"id": info.Id, "sub_mch_name": info.SubMchName, "name": info.Name, "mobile": info.Mobile})
}

// 获取商户信息
func (ctl *separateCtl) List(c *gin.Context) {
	req := request.JsonMap(c)
	subMchId := gconv.String(req["sub_mch_id"])
	if subMchId == "" {
		response.Error(c, "参数缺失【sub_mch_id】", nil)
		return
	}
	info, err := service.SeparateAccounts.Info(c, "*", subMchId)
	if err != nil {
		response.Response(c, 50008, err.Error(), nil)
		return
	}
	response.Success(c, "success", info)

}

// CreateOrder 创建订单
func (ctl *separateCtl) CreateOrder(c *gin.Context) {
	var req model.CreateOrder
	if err := c.ShouldBind(&req); err != nil {
		response.Error(c, valid.GetError(err.(validator.ValidationErrors), req), nil)
		return
	}
	if req.Body == "" {
		req.Body = "商户充值"
	}
	OrderNO, err := service.SeparateAccounts.CreateOrder(c, &req)
	if err != nil {
		response.Error(c, err.Error(), nil)
		return
	}
	response.Success(c, "success", gin.H{
		"order_no": OrderNO,
	})
}

func (ctl *separateCtl) PayOrder(c *gin.Context) {
	req := request.JsonMap(c)
	orderNo := gconv.String(req["order_no"])
	var m tables.CourseOrder
	err := dao.SepatateOrder.Ctx(c).Where("order_no=?", orderNo).Scan(&m)
	if err != nil {
		response.Error(c, err.Error(), nil)
		return
	}
	if m.Id == 0 {
		response.Error(c, "该订单不存在", nil)
		return
	}
	if orderNo == "" {
		response.Error(c, "订单号不能为空", nil)
		return
	}
	order, err := service.SeparateAccounts.PayOrder(c, &m)
	if err != nil {
		response.Error(c, err.Error(), nil)
		return
	}
	response.Success(c, "success", order)
}
func (ctl *separateCtl) Apply(c *gin.Context) {
	var req model.ReqSeparateApply
	if err := c.ShouldBind(&req); err != nil {
		response.Error(c, valid.GetError(err.(validator.ValidationErrors), req), nil)
		return
	}
	if err := service.SeparateAccounts.Apply(c, &req); err != nil {
		response.Error(c, err.Error(), nil)
		return
	}
	response.Success(c, "success", nil)
}
